package com.vf.cloud.scene.mvc.biz.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import com.jfinal.kit.StrKit;
import com.jfinal.plugin.activerecord.Page;
import com.vf.cloud.common.datasource.annotation.Tx;
import com.vf.cloud.common.util.R;
import com.vf.cloud.common.util.UuidUtil;
import com.vf.cloud.pass.common.domain.biz.Scene;
import com.vf.cloud.scene.common.domain.SubLayer;
import com.vf.cloud.scene.common.domain.SubLayerElement;
import com.vf.cloud.scene.common.domain.SubLayerStylePoi;
import com.vf.cloud.scene.mvc.biz.service.ILayerService;

@RestController
@RequestMapping("/scene/biz/layer")
public class LayerController {

	@Autowired
	private ILayerService layerService;

	@GetMapping(value = { "/list" }, produces = "application/json; charset=utf-8")
	public R<Page<SubLayer>> findList(HttpServletRequest request,
			@RequestParam(name = "projectId", required = false, defaultValue = "") String projectId,
			@RequestParam(name = "meunId", required = false, defaultValue = "") String meunId,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords,
			@RequestParam(name = "cur", required = false, defaultValue = "1") int cur,
			@RequestParam(name = "limit", required = false, defaultValue = "10") int limit) throws Exception {
		return layerService.findList(projectId, keywords, cur, limit);
	}

	@RequestMapping(value = "/findAll", method = RequestMethod.GET)
	public R<List<SubLayer>> findALL(@RequestParam(name = "projectId") String projectId,
			@RequestParam(name = "menuId", required = false, defaultValue = "") String menuId,
			@RequestParam(name = "keywords", required = false, defaultValue = "") String keywords) throws Exception {
		
		String sqlExceptSelect = " FROM " + SubLayer.TABLE_NAME + " O  WHERE 1=1 ";
		if (!StrKit.isBlank(keywords)) {
			sqlExceptSelect += " AND ( O.NAME LIKE '%" + keywords + "%' ) ";
		}
		if (!StrKit.isBlank(projectId)) {
			sqlExceptSelect += " AND O.project_id = '" + projectId + "' ";
		}
		if(!StrKit.isBlank(menuId)) {
			sqlExceptSelect += " AND O.ID NOT IN (SELECT LAYER_ID FROM SUB_MENU_LAYER WHERE MENU_ID='"+menuId+"' )";
		}
		sqlExceptSelect += "  ORDER BY O.SORT asc ";
		List<SubLayer> layers = SubLayer.dao.find("SELECT * "+sqlExceptSelect);
		return R.ok(layers);
	}

	@Tx
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public R<SubLayer> save(SubLayer layer) {
		try {

			if (StrKit.isBlank(layer.getName())) {
				return R.failed("图层名称不可以为空！");
			}

			if (StrKit.notBlank(layer.getId())) {
				if (layer.update()) {
					return R.ok(layer);
				} else {
					return R.failed("更新失败！");
				}
			} else {
				layer.setId(UuidUtil.getUUID());
				if (layer.save()) {
					return R.ok(layer);
				} else {
					return R.failed("保存失败！");
				}
			}
		} catch (Exception e) {
			e.printStackTrace();
			return R.failed(e.getMessage());
		}
	}

	@Tx
	@RequestMapping(value = "/deleteById", method = RequestMethod.GET)
	public R<String> deleteById(@RequestParam(name = "id") String id) throws Exception {
		try {
			SubLayer layer = SubLayer.dao.findById(id);
			if (layer == null)
				return R.failed("数据不存在，刷新后尝试!");

			List<SubLayerElement> layerElement = SubLayerElement.dao
					.find("SELECT * FROM " + SubLayerElement.TABLE_NAME + " WHERE LAYER_ID=? ", id);
			if (layerElement != null && layerElement.size() > 0) {
				return R.failed("请先删除图层【" + layer.getName() + "】下元素!");
			}
			if (layer.delete()) {
				SubLayerStylePoi.dao.deleteById(layer.getId());
				return R.ok();
			} else {
				return R.failed("失败！");
			}
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@RequestMapping(value = "/getOptions", method = RequestMethod.GET)
	public R<Object> getOptions() throws Exception {
		try {
			List<Scene> opts = Scene.dao.findAll();
			if (opts == null || opts.size() == 0)
				return R.failed();
			return R.ok(opts);
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@Tx
	@RequestMapping(value = "/findStyleById", method = RequestMethod.GET)
	public R<Object> findStyleById(@RequestParam(name = "id") String id) throws Exception {
		try {
			SubLayer layer = SubLayer.dao.findById(id);
			if (layer == null)
				return R.failed("图层不存在，刷新后尝试!");

			SubLayerStylePoi layerStylePoi = SubLayerStylePoi.dao.findById(id);
			return R.ok(layerStylePoi);
		} catch (Exception e) {
			return R.failed(e.getMessage());
		}
	}

	@Tx
	@RequestMapping(value = "/addPoiStyle", method = RequestMethod.GET)
	public R<Object> addPoiStyle(@RequestParam(name = "templateId") String templateId,
			@RequestParam(name = "layerId") String layerId) throws Exception {
		return layerService.addPoiStyle(templateId, layerId);
	}

	@Tx
	@RequestMapping(value = "/savePoiStyle", method = RequestMethod.POST)
	public R<SubLayerStylePoi> savePoiStyle(MultipartFile file1, MultipartFile file2, SubLayerStylePoi model) {
		return layerService.savePoiStyle(file1, file2, model);
	}

}
